#include "String.h"
using namespace std;

String::String()
{
}

String::~String()
{
}

string String::urlEncode(const string &s)
{
	string sOut;
	return sOut;
}

string String::urlDecode(const string &s)
{
	string buffer = "";

	int len = s.length();

	for (int i = 0; i < len; i++)
	{
		try
		{
			int j = i;
			char ch = s.at(j);

			if (ch == '%')
			{
				char tmpstr[] = "0x0__";
				int chnum;
				tmpstr[3] = s.at(j + 1);
				tmpstr[4] = s.at(j + 2);
				chnum = strtol(tmpstr, NULL, 16);
				buffer += chnum;
				i += 2;
			}
			else
			{
				buffer += ch;
			}
		}
		catch (std::out_of_range & exc)
		{
			printf("%s",exc.what());
		}
	}

	return buffer;
}

//MD5加密函数
char *String::md5(const char *string)
{
	unsigned char buffer[16];
	char *p = (char*) malloc(33);

	MD5((const unsigned char*) string, strlen(string), buffer);

	//MD5_CTX context;
	//MD5_Init(&context);
	//MD5_Update(&context, string, strlen(string));
	//MD5_Final(buffer, &context);

	int i;

	for (i = 0; i < 16; i++)
	{
		sprintf(p + 2 * i, "%02x", buffer[i]);
	}

	p[32] = '\0';

	return p;
}

